package com.hsnn.medstgmini.pro.plan.controller;



import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.DicSys;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.pro.plan.service.WsjPlanManager;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.AreaUtil;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.DicSysUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 卫生局采购计划管理
 * @author wangbing
 */
@Controller
@RequestMapping("/wsjplan")
public class WsjPlanController {
	
	private static final Logger log = Logger.getLogger(WsjPlanController.class);
	
	@Autowired
	private BidProjectManager bidProjectManager;
	
	@Autowired
	private  HttpServletRequest request;
	
	@Autowired
	private WsjPlanManager wsjPlanManager;
	
	@Autowired
	private DicAreaManager dicAreaManager;
	
	@Value("#{sysProp['system.province']}")
	private String sysProvince;
	
	@Value("#{sysProp['system.purProportion']}")
	private String purProportion;
	
	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) request.getSession().getAttribute(Constants.USERINFO);
	}

	
	/**
	 * 
	 *@category 跳转区域采购计划 项目列表
	 *@author wangbing
	 *@date 2015年6月12日 下午3:09:16
	 *@return
	 */
	@RequestMapping("/toWsjProjectList")
	public String toWsjProjectList(){
		log.info("enter toWsjProjectList method");
		return "/pro/plan/wsjProjectList";
	}
	
	/**
	 * @category 获取区域采购计划 项目列表
	 * @author wangbing
	  *@date 2015年6月12日 下午3:09:16
	 * @return
	 */
	@RequestMapping("/getWsjProjectList")
	@ResponseBody
	public Pagination getWsjProjectList() {
		Pagination page = new Pagination(request);
		String name = request.getParameter("name");
		String status = request.getParameter("status");
		if(StringUtils.isNotBlank(name)){
			page.getConditions().put("name", name.trim());
		}
		if(StringUtils.isNotBlank(status)){
			page.getConditions().put("status", status.trim());
		}
		try {
			String healbureaId = getUser().getOrgId();
		    page.getConditions().put("healbureaId", healbureaId);
			wsjPlanManager.getWsjProjectList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 *@category  跳转到采购计划按地区汇总页面
	 *@author wangbing
	 * @throws UnsupportedEncodingException 
	 *@date 2015年6月18日
	 *String
	 */
	@RequestMapping("/toWsjPlanByAreaList")
	public String toWsjPlanByAreaList() throws UnsupportedEncodingException {
		request.setAttribute("projId", request.getParameter("projId"));
		if (StringUtils.isNotBlank(request.getParameter("projName"))) {
		    request.setAttribute("projName", URLDecoder.decode(request.getParameter("projName"), "utf-8"));
		}
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		return "/pro/plan/wsjPlanByArea";
	}
	
	/**
	 * 
	 *@category	获取采购计划按地区列表及查询
	 *@author wangbing
	 *@date 2015年6月18日
	 *Pagination
	 */
	@RequestMapping("/getWsjPlanByAreaList")
	@ResponseBody
	public Pagination getWsjPlanByAreaList(){
		Pagination page = new Pagination(request);
		String projId = request.getParameter("projId");
		String area2 = request.getParameter("area2");
		String area3 = request.getParameter("area3");
		String areaId = AreaUtil.getAreaStartCode(sysProvince, area2, area3);
		try {
			page.getConditions().put("projId", projId);
			page.getConditions().put("areaId", areaId);
			page.getConditions().put("healbureaId", getUser().getOrgId());
			wsjPlanManager.getWsjPlanByAreaList(page);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * 
	 *@category  跳转到采购计划地区汇总详情页面
	 *@author wangbing
	 *@date 2015年6月18日
	 *String
	 */
	@RequestMapping("/toWsjPlanDetailsByArea")
	public String toWsjPlanDetailsByArea() {
		request.setAttribute("projId", request.getParameter("projId"));
		request.setAttribute("areaId", request.getParameter("areaId"));
		try {
			request.setAttribute("projName", URLDecoder.decode(request.getParameter("projName"), "utf-8"));
			request.setAttribute("areaName", URLDecoder.decode(request.getParameter("areaName"), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "/pro/plan/wsjPlanDetailsByArea";
	}
	
	/**
	 * 
	 *@category	获取采购计划按地区汇总列表及查询
	 *@author wangbing
	 *@date 2015年6月18日
	 *Pagination
	 */
	@RequestMapping("/getWsjPlanDetailsByArea")
	@ResponseBody
	public Pagination getWsjPlanDetailsByArea(){
		Pagination page = new Pagination(request);
		String projId = request.getParameter("projId");
		String areaId = request.getParameter("areaId");
		String drugCode = request.getParameter("drugCode");
		String drugName = request.getParameter("drugName");
		String compName = request.getParameter("compName");
		String healbureaId = getUser().getOrgId();
		if(StringUtils.isNotBlank(drugCode)){
			page.getConditions().put("drugCode", drugCode.trim());
		}
		if(StringUtils.isNotBlank(drugName)){
			page.getConditions().put("drugName", drugName.trim());
		}
		if(StringUtils.isNotBlank(compName)){
			page.getConditions().put("compName", compName.trim());
		}
		try {
			page.getConditions().put("projId", projId);
			page.getConditions().put("areaId", areaId);
			page.getConditions().put("healbureaId", healbureaId);
			wsjPlanManager.getWsjPlanDetailsByArea(page);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * 
	 *@category 跳转区域采购计划 项目列表
	 *@author wangbing
	 *@date 2015年6月12日 下午3:09:16
	 *@return
	 */
	@RequestMapping("/toWsjPlanProjectList")
	public String toWsjPlanProjectList(){
		log.info("enter toWsjPlanProjectList method");
		return "/pro/plan/wsjPlanProjectList";
	}
	
	/**
	 * 
	 *@category 获取当前卫生局申报计划列表数据
	 *@author wangbing
	 *@date 2015年6月12日 下午3:09:16
	 *@return
	 */
	@RequestMapping("/getWsjPlanProjectList")
	@ResponseBody
	public Pagination getWsjPlanProjectList(){
		Pagination page = new Pagination(request);
		if (null != request.getParameter("projCode") && !"".equals(request.getParameter("projCode").trim())){
			page.getConditions().put("projCode", request.getParameter("projCode").trim());
		}
		if (null != request.getParameter("name") && !"".equals(request.getParameter("name").trim())){
			page.getConditions().put("name", request.getParameter("name").trim());
		}
		page.getConditions().put("healbureaId", getUser().getOrgId());
		wsjPlanManager.getWsjPlanProjectList(page);
		return page;
	}
	
	/**
	 * 
	 *@category 跳转未申报药品页面
	 *@author wangbing
	 *@date 2015年6月12日 下午3:09:16
	 *@return
	 */
	@RequestMapping("/toNoDeclareDrugList")
	public String toNoDeclareDrugList(){
		try {
			List<DicSys> proCatList = DicSysUtil.getDicListByClassCode(Constants.PRO_METHOD);
			BidProject bp = bidProjectManager.getBidProjectById(request.getParameter("projId"));
			request.setAttribute("projName", bp.getName());
			request.setAttribute("proCatList", proCatList);
			request.setAttribute("projId", request.getParameter("projId"));
			request.setAttribute("healbureaId", getUser().getOrgId());
			request.setAttribute("purProportion", purProportion);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/pro/plan/noDeclareDrugList";
	}
	
	/**
	 * 
	 *@category 获取当前卫生局未申报的药品
	 *@author wangbing
	 *@date 2015年6月12日 下午4:53:01
	 *@return
	 */
	@RequestMapping("/getNoDeclareDrugList")
	@ResponseBody
	public Pagination getNoDeclareDrugList(){
		Pagination page = new Pagination(request);
		if (null != request.getParameter("drugCode") && !"".equals(request.getParameter("drugCode").trim())){
			page.getConditions().put("drugCode", request.getParameter("drugCode").trim());
		}
		if (null != request.getParameter("drugName") && !"".equals(request.getParameter("drugName").trim())){
			page.getConditions().put("drugName", request.getParameter("drugName").trim());
		}
		if (null != request.getParameter("prodPompId") && !"".equals(request.getParameter("prodPompId").trim())){
			page.getConditions().put("prodPompId", request.getParameter("prodPompId").trim());
		}
		
		if (StringUtils.isNotBlank(request.getParameter("compName"))) {
			page.getConditions().put("compName", request.getParameter("compName").trim());
		}
		
		if (null != request.getParameter("projId") && !"".equals(request.getParameter("projId").trim())){
			page.getConditions().put("projId", request.getParameter("projId").trim());
		}
		page.getConditions().put("healbureaId", request.getParameter("healbureaId").trim());
		page = wsjPlanManager.getNoDeclareDrugList(page);
		return page;
	}
	
	@RequestMapping("/toDeclareDrugList")
	public String toDeclareDrugList(){
		request.setAttribute("projId", request.getParameter("projId"));
		
		BidProject bp = bidProjectManager.getBidProjectById(request.getParameter("projId"));
		request.setAttribute("projName", bp.getName());
		return "/pro/plan/declareDrugList";
	}
	
	/**
	 *@category 获取当前卫生局已申报的药品
	 *@author wangbing
	 *@date 2015年6月28日 
	 *@return
	 */
	@RequestMapping("/getDeclareDrugList")
	@ResponseBody
	public Pagination getDeclareDrugList(){

		Pagination page = new Pagination(request);
		if (null != request.getParameter("drugCode") && !"".equals(request.getParameter("drugCode").trim())){
			page.getConditions().put("drugCode", request.getParameter("drugCode").trim());
		}
		if (null != request.getParameter("drugName") && !"".equals(request.getParameter("drugName").trim())){
			page.getConditions().put("drugName", request.getParameter("drugName").trim());
		}
		if (null != request.getParameter("prodPompId") && !"".equals(request.getParameter("prodPompId").trim())){
			page.getConditions().put("prodPompId", request.getParameter("prodPompId").trim());
		}
		try {
			page.getConditions().put("projId", request.getParameter("projId").trim());
			page.getConditions().put("healbureaId", getUser().getOrgId());
			wsjPlanManager.getDeclareDrugList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	//批量
		@RequestMapping("/deleteData")
		@ResponseBody
		public Pagination deleteData(String array){
			Pagination page = new Pagination(request);
			try {
				if (array != null && !"".equals(array)) {
					Gson gson = new Gson();
					Type type = new TypeToken<List<String>>() {
					}.getType();
					List<String> list = gson.fromJson(array, type);
					page.setOperCount(wsjPlanManager.deleteData(list));
				}
				page.setSuccess(true);
			} catch (Exception e) {
				page.setSuccess(false);
				e.printStackTrace();
			}
			return page;
		}
		
		/**
		 * 
		 *@category 
		 *@author wangbing
		 *@date 2015年6月12日 下午6:38:41
		 *@return
		 */
		@RequestMapping("/batchAddDrugData")
		@ResponseBody
		public Pagination batchAddDrugData(String array){
			Pagination page = new Pagination(request);
			try {
				if (array != null && !"".equals(array)) {
					Gson gson = new Gson();
					Type type = new TypeToken<List<Map<String,String>>>() {
					}.getType();
					List<Map<String,String>> list = gson.fromJson(array, type);
					if (list != null && !list.isEmpty()) {
						for (Map<String,String> map : list) {
							map.put("userId", getUser().getId());
							map.put("updDatetime", DateUtil.dateTime(new Timestamp(System.currentTimeMillis())));
						}
						page.setOperCount(wsjPlanManager.batchAddDrugData(list));
						page.setSuccess(true);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				page.setSuccess(false);
			}
			return page;
		}
}
